package com.ktjy.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.crm.entity.Role;
import com.ktjy.crm.entity.User;
import com.ktjy.crm.service.IRoleService;
import com.ktjy.crm.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
public class UserController {
    @Resource
    private IUserService iUserService;
    @Resource
    private IRoleService iRoleService;

    @RequestMapping(value = "/dologin")
    public String doLogin(String usrName, String usrPassword, HttpServletRequest request){
        User user=iUserService.login(usrName,usrPassword);
        if (user!=null){
            request.getSession().setAttribute("loginUser",user);
            return "main";
        }else {
            request.setAttribute("message","登录失败，用户名或者密码错误！");
            return "login";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        return "login";
    }
    @RequestMapping(value = "/user/list")
    public String findUsers( Model model,String usrName
            , @RequestParam(required = false,defaultValue = "0") Long roleId
            ,@RequestParam(required = false,defaultValue = "1")int pageIndex){
        HashMap<String, Object> hashMap=new HashMap<>();
        hashMap.put("usrName",usrName);
        hashMap.put("roleId",roleId);
        hashMap.put("pageIndex",pageIndex);
        hashMap.put("pageSize",3);

        IPage<User> userIPage=iUserService.getUserPage(hashMap);
        model.addAttribute("userPager",userIPage);
        model.addAttribute("usrName",usrName);
        model.addAttribute("roleId",roleId);

        List<Role> roleList=iRoleService.findAllRoles();
        model.addAttribute("roles",roleList);
        return "/user/list";
    }

    @RequestMapping(value = "/user/add")
    public String toAdd(Model model){
        List<Role> roleList=iRoleService.findAllRoles();
        model.addAttribute("roles",roleList);
        return "user/add";
    }

    @PostMapping(value = "/user/add")
    public String addUser(User user){
        int result=iUserService.addUser(user);
        if (result>0){
            return "redirect:/user/list";
        }else {
            return "redirect:/user/add";
        }
    }

    @GetMapping(value = "/user/edit")
    public String toEdit(Long usrId,Model model){
        User user=iUserService.getUserById(usrId);
        model.addAttribute("user",user);

        List<Role> roleList=iRoleService.findAllRoles();
        model.addAttribute("roles",roleList);
        return "user/edit";
    }

    @PostMapping(value = "/user/edit")
    public String editUser(User user){
        int result=iUserService.updateUser(user);
        if (result>0){
            return "redirect:/user/list";
        }else {
            return "redirect:/user/edit";
        }
    }

    @RequestMapping("/user/del")
    @ResponseBody
    public Map del(Long usrId){
        int result=iUserService.deleteUser(usrId);
        Map map=new HashMap();
        if (result>0){
            map.put("delResult",true);
        }else {
            map.put("delResult",false);
        }
        return map;
    }

}
